package com.erp.gather.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.gather.domain.ErpCollectProductDiagnosisRecord;
import com.ruoyi.gather.api.domain.ErpSellerStoreProduct;
import com.erp.gather.domain.ErpSellerStoreProductDiagnosisRecord;
import com.erp.gather.domain.bo.ErpSellerStoreProductDiagnosisRecordBo;
import com.erp.gather.domain.vo.ErpSellerStoreProductDiagnosisRecordVo;
import com.erp.gather.mapper.ErpCollectProductDiagnosisRecordMapper;
import com.erp.gather.mapper.ErpSellerStoreProductDiagnosisRecordMapper;
import com.erp.gather.mapper.ErpSellerStoreProductMapper;
import com.erp.gather.service.IErpSellerStoreProductDiagnosisRecordService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 在线商品检测记录Service业务层处理
 *
 * @author erp
 * @date 2023-12-08
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStoreProductDiagnosisRecordServiceImpl implements IErpSellerStoreProductDiagnosisRecordService {

    private final ErpSellerStoreProductDiagnosisRecordMapper baseMapper;
    private final ErpCollectProductDiagnosisRecordMapper erpCollectProductDiagnosisRecordMapper;
    private final ErpSellerStoreProductMapper erpSellerStoreProductMapper;
//    private final IAeProductEditService iAeProductEditService;

    /**
     * 查询在线商品检测记录
     */
    @Override
    public ErpSellerStoreProductDiagnosisRecordVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询在线商品检测记录列表
     */
    @Override
    public TableDataInfo<ErpSellerStoreProductDiagnosisRecordVo> queryPageList(ErpSellerStoreProductDiagnosisRecordBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStoreProductDiagnosisRecord> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStoreProductDiagnosisRecordVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询在线商品检测记录列表
     */
    @Override
    public List<ErpSellerStoreProductDiagnosisRecordVo> queryList(ErpSellerStoreProductDiagnosisRecordBo bo) {
        LambdaQueryWrapper<ErpSellerStoreProductDiagnosisRecord> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStoreProductDiagnosisRecord> buildQueryWrapper(ErpSellerStoreProductDiagnosisRecordBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<ErpSellerStoreProductDiagnosisRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getProductId() != null, ErpSellerStoreProductDiagnosisRecord::getProductId, bo.getProductId());
        lqw.eq(StringUtils.isNotBlank(bo.getOperateStatus()), ErpSellerStoreProductDiagnosisRecord::getOperateStatus, bo.getOperateStatus());
        lqw.eq(bo.getProductImpact() != null, ErpSellerStoreProductDiagnosisRecord::getProductImpact, bo.getProductImpact());
        lqw.eq(StringUtils.isNotBlank(bo.getMainPicture()), ErpSellerStoreProductDiagnosisRecord::getMainPicture, bo.getMainPicture());
        lqw.eq(StringUtils.isNotBlank(bo.getProductTitle()), ErpSellerStoreProductDiagnosisRecord::getProductTitle, bo.getProductTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getProblemDescription()), ErpSellerStoreProductDiagnosisRecord::getProblemDescription, bo.getProblemDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getProblemType()), ErpSellerStoreProductDiagnosisRecord::getProblemType, bo.getProblemType());
        lqw.eq(StringUtils.isNotBlank(bo.getOptimizationSuggestion()), ErpSellerStoreProductDiagnosisRecord::getOptimizationSuggestion, bo.getOptimizationSuggestion());
        lqw.eq(bo.getStoreId() != null, ErpSellerStoreProductDiagnosisRecord::getStoreId, bo.getStoreId());
        lqw.eq(bo.getStatus() != null, ErpSellerStoreProductDiagnosisRecord::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增在线商品检测记录
     */
    @Override
    public Boolean insertByBo(ErpSellerStoreProductDiagnosisRecordBo bo) {
        ErpSellerStoreProductDiagnosisRecord add = BeanUtil.toBean(bo, ErpSellerStoreProductDiagnosisRecord.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改在线商品检测记录
     */
    @Override
    public Boolean updateByBo(ErpSellerStoreProductDiagnosisRecordBo bo) {
        ErpSellerStoreProductDiagnosisRecord update = BeanUtil.toBean(bo, ErpSellerStoreProductDiagnosisRecord.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStoreProductDiagnosisRecord entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除在线商品检测记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public R<Object> diagnosisProductTask(Long id) {
        ErpCollectProductDiagnosisRecord collectProduct = erpCollectProductDiagnosisRecordMapper.selectById(id);
        if (ObjectUtils.isEmpty(collectProduct)) {
            return R.fail("任务不存在");
        }
        //获取需要处理的记录
        List<ErpSellerStoreProductDiagnosisRecord> sellerStoreProductDiagnosisRecordList = baseMapper.diagnosisProductTaskByCollectProductId(
                collectProduct.getCollectProductId()
        );
        if (sellerStoreProductDiagnosisRecordList.size() == 0) {
            return R.fail("没有需要处理的信息");
        }

        String optimizationPicture = new String();

        for (ErpSellerStoreProductDiagnosisRecord record : sellerStoreProductDiagnosisRecordList) {
            ErpSellerStoreProduct product = erpSellerStoreProductMapper.selectOne(
                    new LambdaQueryWrapper<ErpSellerStoreProduct>()
                            .eq(ErpSellerStoreProduct::getAeProductId, record.getProductId())
            );
            String problemType = record.getProblemType();
            switch (problemType) {
                case "lack_white_image":
                    //缺少白底图
                    optimizationPicture = collectProduct.getWhite();
                    //调用修改接口
                    product.setWhite(optimizationPicture);
                    erpSellerStoreProductMapper.updateById(product);
                    //调用修改接口
                    //TODO AE接口调用
//                    iAeProductEditService.productEditAndUploadPicBank(product.getId(),6);
                    break;
                case "maketing_imgs_3_4":
                    //缺少营销图
                    optimizationPicture = collectProduct.getMarketing();
                    //调用修改接口
                    product.setMarketing(optimizationPicture);
                    erpSellerStoreProductMapper.updateById(product);
                    //调用修改接口
                    //TODO AE接口调用
//                    iAeProductEditService.productEditAndUploadPicBank(product.getId(),6);
                    break;
                case "lack_mobile_desc":
                    //缺少独立详描
                    break;
            }

            record.setOptimizationPicture(optimizationPicture);
            record.setStatus(1);
            baseMapper.updateById(record);

        }


        collectProduct.setStatus(3);
        erpCollectProductDiagnosisRecordMapper.updateById(collectProduct);
        return R.ok("提交优化成功");
    }
}
